//
//  MSSearchDefaultCell.m
//  MSJD
//
//  Created by 箭军刘 on 2018/11/3.
//  Copyright © 2018 xiangbo. All rights reserved.
//

#import "MSSearchDefaultCell.h"

#import "MSSearchButton.h"

NSInteger const kMSSearchButtonDefaultTag = 12341234;

@interface MSSearchDefaultCell()

@property (nonatomic, strong) UIView *showView;
@property (nonatomic, copy) NSArray *list;

@property (nonatomic, assign) NSInteger selectBtnTag; // 选中按钮的tag

@end

@implementation MSSearchDefaultCell

#pragma mark - Init Method

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)refreshSearchDefaultCellWithList:(NSArray *)list {
    if (self.list && [self.list isEqual:list]) {
        // 重复刷新，数据一样，不需要刷新
        return;
    }
    if (self.showView) {
        [self.showView removeFromSuperview];
        self.showView = nil;
    }
    self.list = list;
    [self addSubViewsToShowView:list];
}

+ (CGFloat)caculateSearchDefaultHeight:(NSArray *)list {
    CGFloat height = 44;
    CGFloat maxWidth = MS_SCREEN_WIDTH - 2 * 20;
    CGFloat totalWidth = 0.0;
    for (NSString *title in list) {
        CGFloat buttonWidth = [MSSearchButton caculateButtonHeightWihtTitle:title];
        if ((totalWidth + buttonWidth) < maxWidth) {
            // 暂未超过一行
            totalWidth += buttonWidth + 10;
        } else {
            // 超过了一行
            height += 44;
            totalWidth = buttonWidth;
        }
    }
    // 多出来的高度
    CGFloat space = 24;
    return height + space;
}

#pragma mark - Event Response
/** 搜索按钮点击 */
- (void)searchItemButtonClick:(MSSearchButton *)sender {
    NSInteger index = sender.tag - kMSSearchButtonDefaultTag;
    if (index >= self.list.count) {
        return;
    }
    if (self.selectBtnTag != index) {
        MSSearchButton *oldBtn = (MSSearchButton *)[self.showView viewWithTag:(kMSSearchButtonDefaultTag + self.selectBtnTag)];
        oldBtn.selected = NO;
    }
    sender.selected = YES;
    NSString *title = [MSSafeObject stringInfo:self.list[index]];
    if (self.SelectSearchItemBlock) {
        self.SelectSearchItemBlock(title);
    }
}

#pragma mark - Private Method
/** 加载按钮 */
- (void)addSubViewsToShowView:(NSArray *)list {
    
    self.selectBtnTag = -1;
    
    CGFloat height = [MSSearchDefaultCell caculateSearchDefaultHeight:list];
    self.showView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MS_SCREEN_WIDTH, height)];
    self.showView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.showView];
    
    // 添加按钮
    CGFloat buttonX = 20;
    CGFloat buttonY = 12;
    CGFloat buttonHeight = 32;
    CGFloat totalWidth = 0;
    CGFloat maxWidth = MS_SCREEN_WIDTH - 2 * 20;
    NSInteger index = 0;
    for (NSString *title in list) {
        CGFloat buttonWidth = [MSSearchButton caculateButtonHeightWihtTitle:title];
        // 计算x 和 y
        if ((totalWidth + buttonWidth) > maxWidth) {
            buttonX = 20;
            buttonY += 44;
            totalWidth = 0;
        }
        MSSearchButton *searchBtn = [MSSearchButton initSearchButtonWithTitle:title];
        [searchBtn setFrame:CGRectMake(buttonX, buttonY, buttonWidth, buttonHeight)];
        [searchBtn addTarget:self action:@selector(searchItemButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        searchBtn.layer.cornerRadius = 16.0;
        searchBtn.tag = kMSSearchButtonDefaultTag + index;
        [self.showView addSubview:searchBtn];
        
        buttonX += buttonWidth + 10;
        totalWidth += buttonWidth + 10;
        index ++;
    }
}

#pragma mark -  Memory Method

- (void)dealloc {
    NSLog(@"%s", __func__);
}


@end
